require.config({
    paths: {
        "common": "common/common"
    }
    // baseUrl: 'js/common'
});

require(['common', 'dept'], function(defaultStr, queryData) {
    var baseUrl = defaultStr();
    /*----------
     *ajax数据请求
     *部门页面管理————补助页面
     *部门页面管理————补发页面
     *默认打开该页的时候就已经知道了是那个部门
     *并且只显示该部门的信息
     */

    var user_table = new Vue({
        el: '#main-data',
        data: {
            items: [],
            query: {
                "rows": 10,
                "page": 1
            },
            delItem: '',
            editItem: '',
            roles: [{
                id: 0,
                name: "管理员"
            }, {
                id: 1,
                name: "老师"
            }, {
                id: 2,
                name: "学生"
            }],
            depts: [],
            newItem: '',
            url: {
                'queryUser': baseUrl + '/base/user/list.do',
                'addUser': baseUrl + '/base/user/add.do',
                'delUser': baseUrl + '/base/user/delete.do',
                'getDepts': baseUrl + '/base/dept/list.do'
            }
        },
        methods: {
            /*初始化页面*/
            init: function() {
                var _self = this;
                _self.queryData();
                _self.queryDepts();
            },
            /*用户提交查询信息，查询用户信息*/
            queryData: function() {
                var _self = this;
                /*提交数据到服务器 query是要查询的数据对象*/

                $.ajax({
                    type: "POST",
                    url: _self.url.queryUser,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(_self.query),
                    dataType: "json",
                    success: function(result) {
                        _self.items = [];
                        console.log(result);
                        $.each(result.result, function(i, val) {
                            _self.items.push(val)
                        });
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            /*查询数据*/
            queryDepts: function() {
                var _self = this;
                var data = {
                    "rows": 5,
                    "page": 1,
                };
                $.ajax({
                    type: "POST",
                    url: _self.url.getDepts,
                    data: JSON.stringify(data),
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        console.log(result);
                        _self.depts = [];
                        $.each(result.result, function(i, value) {
                            _self.depts.push(value);
                        })
                        return _self.items;
                    }
                });
            },
            delUser: function(id, loginName, deptId) {
                var _self = this;
                _self.delItem = {
                    "id": id,
                    "name": loginName,
                    "deptId": deptId
                }
            },
            confirmDel: function() {
                var _self = this;
                var data = {
                    "loginName": this.delItem.loginName
                };
                $.ajax({
                    type: "POST",
                    url: _self.url.delUser,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),
                    dataType: "json",
                    success: function(result) {
                        alert(result.message);
                        setTimeout(function() {
                            $('#myModal-del').modal('hide');
                            _self.queryData();
                        }, 1000);
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            /*编辑用户角色*/
            editUser: function(id, loginName, deptId) {
                var _self = this;
                _self.eitItem = {
                    "id": id,
                    "name": loginName,
                    "deptId": deptId
                }
            },
            confirmEdit: function() {

            },
            addUser: function() {
                var _self = this;
                $.ajax({
                    type: "POST",
                    url: _self.url.addUser,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(_self.newItem),
                    dataType: "json",
                    success: function(result) {
                        alert(result.message);
                        setTimeout(function() {
                            $('#myModal-add').modal('hide');
                            _self.queryData();
                        }, 1000);
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            }
        }
    });
    user_table.init();
});